﻿

using System;
using System.Collections.Generic;
using System.Linq;
using Web.Models;
using Model.Core.DataBase;
using Web.Models.AnalysisData;
using Web.Models.CoreData;


namespace Web.BML.CoreData
{
    /// <summary>
    /// 船舶信息
    /// </summary>
    public class ShipOp
    {

        private static ShipOp instance;
        /// <summary>
        /// 构造函数
        /// </summary>
        private ShipOp()
        {

        }
        /// <summary>
        /// 单例模式实例化
        /// </summary>
        /// <returns></returns>
        public static ShipOp GetInstance()
        {
            if (instance == null)
            {
                instance = new ShipOp();
            }
            return instance;
        }

        

        /// <summary>
        ///    查询用户的船
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public List<ShipInfo> QueryShips(string userId)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                User user = db.Queryable<User>().FirstOrDefault(m => m.Id == userId);
                List<ShipInfo> shipInfos = new List<ShipInfo>();
                var list = db.Queryable<ShipInfo>().ToList();
                foreach (var mmsi in user.MMSI.Split(','))
                {
                    var ship = list.FirstOrDefault(m => m.MMSI == mmsi || m.Value.Contains(mmsi));
                    if (ship != null)
                    {
                        shipInfos.Add(ship);
                    }
                }

                return shipInfos;
            }
        }

       






        /// <summary>
        /// 查询所有船
        /// </summary>
        /// <returns></returns>
        public List<ShipInfo> QueryAllShips()
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                return db.Queryable<ShipInfo>().ToList();
            }
        }

        /// <summary>
        /// 修改船信息
        /// </summary>
        /// <param name="shipList"></param>
        public bool ModifShips(List<ShipInfo> shipList)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                foreach (var ship in shipList)
                {
                        ShipInfo model = db.Queryable<ShipInfo>().Where(m => m.MMSI == ship.MMSI || m.Value.Contains(ship.MMSI)).FirstOrDefault();
                        if (model == null)
                        {
                            db.GetOperator<ShipInfo>().Create(ship);
                        }
                        else
                        {
                            model.Name = ship.Name;
                            model.Value = ship.Value;
                            model.MMSI = ship.MMSI;
                            db.GetOperator<ShipInfo>().Modify(model);
                        }
                }
                return db.SaveAllChange()==shipList.Count;
            }
        }

       


    }


}

